 

package com.zdkj.subway.rca.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zdkj.common.core.constant.CommonConstants;
import com.zdkj.common.core.util.R;
import com.zdkj.common.log.annotation.SysLog;
import com.zdkj.subway.rca.entity.SwCkOthenTicketRevenueManagement;
import com.zdkj.subway.rca.entity.SwCkOthenTicketRevenueManagementLog;
import com.zdkj.subway.rca.service.SwCkOthenTicketRevenueManagementLogService;
import com.zdkj.subway.rca.service.SwCkOthenTicketRevenueManagementService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.time.format.DateTimeFormatter;


/**
 * 报表调账-机场城际票务收入管理日志表
 *
 * @author stw
 * @date 2020-10-31 08:14:50
 */
@RestController
@AllArgsConstructor
@RequestMapping("/swckothenticketrevenuemanagementlog" )
@Api(value = "swckothenticketrevenuemanagementlog", tags = "报表调账-机场城际票务收入管理日志表管理")
public class SwCkOthenTicketRevenueManagementLogController {

    private final SwCkOthenTicketRevenueManagementLogService swCkOthenTicketRevenueManagementLogService;

    @Autowired
    private final SwCkOthenTicketRevenueManagementService swCkOthenTicketRevenueManagementService;
    /**
     * 分页查询
     *
     * @param page 分页对象
     * @return
     */
    @ApiOperation(value = "分页查询", notes = "分页查询")
    @GetMapping("/page")
    public R getSwCkOthenTicketRevenueManagementLogPage(Page page, String importStartDate, String importEndDate, String beginTime, String endTime) {
        SwCkOthenTicketRevenueManagementLog swCkOthenTicketRevenueManagementLog = new SwCkOthenTicketRevenueManagementLog();
        if(StringUtils.isNotEmpty(beginTime) && StringUtils.isNotEmpty(endTime)){
            return R.ok(swCkOthenTicketRevenueManagementLogService.page(page, Wrappers.query(swCkOthenTicketRevenueManagementLog)
                    .select("id, uuid, begin_time, end_time, submit_person_id, submit_person_code, submit_person_name, dept_id, creator_id, creator_name, MAX(create_time) as createTime, update_time, del_flag")
                    .or(Wrapper -> Wrapper.le("begin_time",beginTime).between("end_time",beginTime,endTime).eq("del_flag","0"))
                    .or(Wrapper -> Wrapper.le("end_time",endTime).between("begin_time",beginTime,endTime).eq("del_flag","0"))
                    .or(Wrapper -> Wrapper.gt("end_time",endTime).between("begin_time",beginTime,endTime).eq("del_flag","0"))
                    .groupBy("begin_time","end_time")
                    .orderByDesc("create_time")
            ));
        }else {
            return R.ok(swCkOthenTicketRevenueManagementLogService.page(page, Wrappers.query(swCkOthenTicketRevenueManagementLog)
                    .select("id, uuid, begin_time, end_time, submit_person_id, submit_person_code, submit_person_name, dept_id, creator_id, creator_name, MAX(create_time) as createTime, update_time, del_flag")
                    .eq(StringUtils.isNotEmpty(beginTime),"begin_time",beginTime)
                    .eq(StringUtils.isNotEmpty(endTime),"end_time",endTime)
                    .eq("del_flag","0")
                    .groupBy("begin_time","end_time")
                    .orderByDesc("create_time")
            ));
        }


    }

    /**
     * 导出模板
     * @param
     * @return
     * @throws IOException
     */
    @ApiOperation(value = "导出模板", notes = "导出模板")
    @GetMapping("/exportTamplate")
    @PreAuthorize("@pms.hasPermission('sw_ck_othen_ticket_revenue_management_import')")
    public void exportTamplate(HttpServletResponse response) {
        swCkOthenTicketRevenueManagementLogService.exportTamplates(response);
    }

    @ApiOperation(value = "导入", notes = "导入")
    @SysLog("导入机场城际票务收入")
    @PostMapping(value = "/saveBatch", headers = "content-type=multipart/form-data")
    @PreAuthorize("@pms.hasPermission('sw_ck_othen_ticket_revenue_management_import')")
    public R saveBatch(@RequestParam(value = "file") MultipartFile file) throws Exception {
        return R.ok((Object) this.swCkOthenTicketRevenueManagementLogService.saveBatchWithExcel(file));
    }

    /**
     * 浏览
     * @return
     */
    @ApiOperation(value = "浏览，（id:浏览的那条数据的ID）", notes = "浏览，（id：浏览的那条数据的ID）")
    @GetMapping("/listPreview" )
    @PreAuthorize("@pms.hasPermission('sw_ck_othen_ticket_revenue_management_import')")
    public R listPreview(Long id) throws Exception {
        if(id == null){
            return R.ok(CommonConstants.FAIL);
        }
		

        SwCkOthenTicketRevenueManagementLog logObj =  this.swCkOthenTicketRevenueManagementLogService.getById(id);
        SwCkOthenTicketRevenueManagement swCkOthenTicketRevenueManagement = new SwCkOthenTicketRevenueManagement();
		String startDate =  logObj.getBeginTime().format(DateTimeFormatter.ofPattern("yyyy-MM-dd"));		
		String endDate =    logObj.getEndTime().format(DateTimeFormatter.ofPattern("yyyy-MM-dd"));

		QueryWrapper<SwCkOthenTicketRevenueManagement> queryWrapper = new QueryWrapper<SwCkOthenTicketRevenueManagement>();
        queryWrapper.between("statistics_Date", startDate, endDate);

        //swCkOthenTicketRevenueManagement.setLogId(logObj.getUuid());
		return R.ok(swCkOthenTicketRevenueManagementService.list(queryWrapper));        

    }

    /**
     * 导出
     * @param
     * @return
     */
    @ApiOperation(value = "导出", notes = "导出")
    @GetMapping({"/export"})
    @SysLog("机场城际票务收入-导出")
    @ApiImplicitParams(
            {
                    @ApiImplicitParam(name = "startDate", value = "开始日期：yyyy-MM-dd", dataTypeClass = String.class,required = true),
                    @ApiImplicitParam(name = "endDate", value = "结束日期：yyyy-MM-dd", dataTypeClass = String.class,required = true)
            }
    )
    public void export(String startDate,String endDate, HttpServletResponse response) {
        if(StringUtils.isEmpty(startDate) ||  StringUtils.isEmpty(endDate)){
            return;
        }
        swCkOthenTicketRevenueManagementLogService.export(startDate,endDate,response);
    }
}
